﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Reflection;

namespace Common
{
    public static class Property
    {
        public static string NameFor<TSource,TDest>(Expression<Func<TSource,TDest>> evaluation)
        {
            return NameFor(evaluation as Expression);
        }

        public static string NameFor(Expression evaluation)
        {
            var lambda = evaluation as LambdaExpression;
            var expression = lambda.SafeGet(x=>x.Body) as MemberExpression;
            if (expression == null || expression.Member.MemberType != MemberTypes.Property)
                throw new ArgumentException("Expected property, and received " + lambda);

            return expression.Member.Name;
        }
    }
}
